package com.feeyo.groundservice.recordnfc.activity

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Box
import androidx.compose.runtime.mutableStateOf
import com.feeyo.groundservice.base_util.FyLog
import com.feeyo.groundservice.base_util.getColor
import com.feeyo.groundservice.base_util.registerEventBus
import com.feeyo.groundservice.base_util.toJson
import com.feeyo.groundservice.base_util.toastCenter
import com.feeyo.groundservice.base_util.unregisterEventBus
import com.feeyo.groundservice.recordnfc.activity.view.MainView
import com.feeyo.groundservice.recordnfc.constant.AIRPORT
import com.feeyo.groundservice.recordnfc.constant.IATAS
import com.feeyo.groundservice.recordnfc.constant.RecordCardPath
import com.feeyo.groundservice.recordnfc.constant.RecorderCardBrodCastNFCEvent
import com.feeyo.groundservice.recordnfc.constant.TOKEN
import com.feeyo.groundservice.recordnfc.constant.UID
import com.feeyo.groundservice.recordnfc.constant.USE_NFC
import com.feeyo.groundservice.recordnfc.constant.getNfcId
import com.feeyo.groundservice.recordnfc.constant.statusBarColor
import com.feeyo.groundservice.recordnfc.constant.token
import com.feeyo.groundservice.recordnfc.constant.uid
import com.feeyo.nfc.NfcUtil
import com.therouter.router.Autowired
import com.therouter.router.Route
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 *作者：70893 on 2025-10-07
 *描述：录卡主页
 *
 * 说明：需要定义机场，IATAS站点列表
 */
@Route(path = RecordCardPath.RecorderCardActivity, description = "录卡主页")
internal class RecorderCardActivity : BaseRecordCardActivity() {

    private var nfcUtil: NfcUtil? = null


    private val nfcId = mutableStateOf("")

    @Autowired(name = USE_NFC)
    @JvmField
    var useNfc: Boolean = true//是否直接使用nfc模式，有的nfcId可以使用宿主APP广播获取

    @Autowired(name = AIRPORT)
    @JvmField
    var airport: String? = null//有些机场的nfc需要特殊处理的

    @Autowired(name = IATAS)
    @JvmField
    var iatas: List<String> = emptyList()//站点列表

    @Autowired(name = UID)
    @JvmField
    var userUid: String? = null

    @Autowired(name = TOKEN)
    @JvmField
    var accessToken: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStatusBarColor(statusBarColor.getColor(), false)
        if (useNfc) {
            nfcUtil = NfcUtil(this)
        }

        assert(airport != null){
            toastCenter("请传入机场三字码")
            finish()
            return
        }
        assert(iatas.isNotEmpty()){
            toastCenter("请传入站点列表")
            finish()
            return
        }
        userUid?.let {
            uid = it
        } ?: kotlin.run {
            toastCenter("请登录")
            finish()
            return
        }
        accessToken?.let {
            token = it
        } ?: kotlin.run {
            toastCenter("请传入token")
            finish()
            return
        }

        Log.e(TAG, "userUid:$userUid,accessToken:$accessToken,airport:$airport,iatas:${iatas.toJson()}")

        initView()
    }

    override fun onResume() {
        super.onResume()
        registerEventBus()
        nfcUtil?.onResume()
    }

    override fun onPause() {
        super.onPause()
        unregisterEventBus()
        nfcUtil?.onPause()
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        val nfc = nfcUtil?.getNfcIdHex(intent)?.getNfcId(airport!!)?.uppercase()
        nfc?.let {
            nfcId.value = it
        } ?: toastCenter("没有获取到NFC卡ID")

        FyLog.e(TAG, "获取的NFC ID：$nfc")

    }

    private fun initView() {
        setContent { MainView(nfcId = nfcId, iatas = iatas) }
    }

    // 接收nfc消息的
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun broadcastNfc(event: RecorderCardBrodCastNFCEvent) {
        nfcId.value = event.nfcId
    }
}